<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\Admin\ClassBaseTime;
use App\Http\Requests\Admin\ClassBaseTimeController\doRule;

class ClassBaseTimeController extends Controller
{
    public function index()
    {

        $result = [
            'result'=> ClassBaseTime::lists()->paginate(6),
            'week'=> ClassBaseTime::week_result(),
            'week_get'=>request()->get('week')
        ];

        return view('admin.class_base_time.index', $result);
    }

    public function add()
    {

        $result = [
            'week'=>'',
            'week_lists'=> ClassBaseTime::week_result()
        ];

        return view('admin.class_base_time.edit', $result);

    }

    public function edit(Request $request, $id)
    {

        $info = ClassBaseTime::info($id);

        $result = [
            'id'=>$id,
            'result'=> $info,
            'week'=> $info['week'],
            'week_lists'=> ClassBaseTime::week_result()
        ];

        return view( 'admin.class_base_time.edit', $result);

    }

    public function do(doRule $request)
    {
        if ($request->ajax()) {

            $input = $request->all();

            $input['duration'] = ClassBaseTime::duration($input['time_start'], $input['time_end']);

            if (empty($input['id'])) {
                unset($input['id']);
                ClassBaseTime::add($input);
            } else {
                ClassBaseTime::edit($input['id'],$input);
            }

            return self::success([]);

        }
    }
}
